package com.wowangz.cms.syscore.autotask.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;

import com.wowangz.cms.common.utils.ParameterConstant;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.dailyoperation.messageswitch.entity.MessageSwitch;
import com.wowangz.cms.dailyoperation.messageswitch.service.MessageSwitchService;
import com.wowangz.cms.dailyoperation.store.pojo.StoreRedisInfo;
import com.wowangz.cms.dailyoperation.store.service.StoreService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.utils.DataPageUtil;
import com.wowangz.cms.syscore.utils.JedisUtil;

public class StoreInfoSync extends BaseJob{

	@SuppressWarnings("rawtypes")
	@Override
	public void innerExecute(JobExecutionContext context) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("statusid", SysConstant.RecordStatus.VALID);
		StoreService storeService = (StoreService) IssTransactionContainer.getInstance(StoreService.class, false);
		MessageSwitchService messageSwitchService = (MessageSwitchService) IssTransactionContainer.getInstance(MessageSwitchService.class, false);
		long count=storeService.findStoreRedisCount(map);
		int page=DataPageUtil.totalPages(Long.valueOf(count).intValue());
		Map mesaageParmMap=storeService.getSysparameterValueByCode(ParameterConstant.SMSSERVER_MESAAGE);
		String smsvalue = (String) mesaageParmMap.get(ParameterConstant.SMSSERVER_MESAAGE);//短信开关
		String emailvalue = (String) mesaageParmMap.get(ParameterConstant.SMSSERVER_MESAAGE);//短信开关
		for (int i = 1; i < page+1; i++) {
			int pageStartRow=DataPageUtil.pageNum *(i-1);
			int pageEndRow=DataPageUtil.pageNum ;
			map.put("startrow",pageStartRow);
			map.put("endrow",pageEndRow);
			List<StoreRedisInfo> list = storeService.findStoreRedisInfo(map);
			for (StoreRedisInfo storeInfo : list){
				map.put("storeid",storeInfo.getStoreid());
				List<MessageSwitch> listMessageSwitch=messageSwitchService.queryMessageSwitchForList(map);
				map.remove("storeid");
				//主开关
				storeInfo.setSmsswitch(String.valueOf(SysConstant.isMessageSwitch.NO));
				storeInfo.setEmailswitch(String.valueOf(SysConstant.isMessageSwitch.NO));
				//子开关
				storeInfo.setLoginswitch(String.valueOf(SysConstant.isMessageSwitch.NO));
				storeInfo.setInstructionswitch(String.valueOf(SysConstant.isMessageSwitch.NO));
				storeInfo.setAgreementswitch(String.valueOf(SysConstant.isMessageSwitch.NO));
				storeInfo.setServiceswitch(String.valueOf(SysConstant.isMessageSwitch.NO));
				if(smsvalue!=null && (ParameterConstant.SMSSERVER_MESAAGE_YES).equals(smsvalue)){
					storeInfo.setSmsswitch(String.valueOf(SysConstant.isMessageSwitch.YES));
				}
				if(emailvalue!=null && (ParameterConstant.SMSSERVER_MESAAGE_YES).equals(emailvalue)){
					storeInfo.setEmailswitch(String.valueOf(SysConstant.isMessageSwitch.YES));
				}
				for (MessageSwitch messageSwitch : listMessageSwitch) {
					if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.LOGIN_SWITCH){
						//登录短信
						storeInfo.setLoginswitch(String.valueOf(messageSwitch.getIsswitch()));
					}else if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.INSTRUCTION_SWITCH){
						//指令短信
						storeInfo.setInstructionswitch(String.valueOf(messageSwitch.getIsswitch()));
					}else if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.AGREENT_SWITCH){
						//协议短信
						storeInfo.setAgreementswitch(String.valueOf(messageSwitch.getIsswitch()));
					}else if(messageSwitch.getSmstype()!=null && messageSwitch.getSmstype()==SysConstant.MessageSwitchType.SERVICE_SWITCH){
						//邮件
						storeInfo.setServiceswitch(String.valueOf(messageSwitch.getIsswitch()));
					}
				}
				JedisUtil.insertPublicDataObject(storeInfo.getStoreid().toString(), storeInfo, JedisUtil.STORE_INFO);
			}
		}
	}
	
	
}
